package epitech.pfa.pocketrhythm;

import android.R.bool;
import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.RelativeLayout;
import android.widget.GridView;



public class MainActivity extends Activity 
{
	private		DrawRectangle	dR0, dR1, dR2, dR3, dR4, dR5, dR6, dR7;
	Button buttonStop;
	private		RelativeLayout	_mainLayout;
    OnClickListener				_b1click, _b2click, _b3click, _b4click,
								_b5click, _b6click, _b7click, _b8click;
    Integer[] imageIDs={
            R.drawable.rectangle_noir,  R.drawable.rectangle_noir,
            R.drawable.rectangle_noir,  R.drawable.rectangle_noir,
            R.drawable.rectangle_noir,  R.drawable.rectangle_noir,
            R.drawable.rectangle_noir,  R.drawable.rectangle_noir,
            R.drawable.rectangle_noir,  R.drawable.rectangle_noir,
            R.drawable.rectangle_noir,  R.drawable.rectangle_noir,
            R.drawable.rectangle_noir,  R.drawable.rectangle_noir,
            R.drawable.rectangle_noir,  R.drawable.rectangle_noir,
    };
    
//    Integer[] imgPlay={
//            R.drawable.rectangle_noir,  R.drawable.rectangle_noir,
//            R.drawable.rectangle_noir,  R.drawable.rectangle_noir,
//            R.drawable.rectangle_noir,  R.drawable.rectangle_noir,
//            R.drawable.rectangle_noir,  R.drawable.rectangle_noir,
//            R.drawable.rectangle_noir,  R.drawable.rectangle_noir,
//            R.drawable.rectangle_noir,  R.drawable.rectangle_noir,
//            R.drawable.rectangle_noir,  R.drawable.rectangle_noir,
//            R.drawable.rectangle_noir,  R.drawable.rectangle_noir,
//            R.drawable.rectangle_noir,  R.drawable.rectangle_noir,
//            R.drawable.rectangle_noir,  R.drawable.rectangle_noir,
//            R.drawable.rectangle_noir,  R.drawable.rectangle_noir,
//            R.drawable.rectangle_noir,  R.drawable.rectangle_noir,
//            R.drawable.rectangle_noir,  R.drawable.rectangle_noir,
//            R.drawable.rectangle_noir,  R.drawable.rectangle_noir,
//            R.drawable.rectangle_noir,  R.drawable.rectangle_noir,
//            R.drawable.rectangle_noir,  R.drawable.rectangle_noir,
//    };
      
	MyMusicThread[]				_musicThreadsArray;

	OnClickListener				splashListener = new OnClickListener(){
		public void onClick(View v){
			onAfterSplashScreen();
			}
		};
    OnClickListener        		playListener = new OnClickListener(){
           	public void onClick(View v){
           		onPlay();
           	}
        };
    OnClickListener        		stopListener = new OnClickListener(){
           	public void onClick(View v){
           		onEdition();
           	}
        };

    OnClickListener				loadListener = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			onLoad();
			
		}
	};	

  public class ImageAdapter extends BaseAdapter {
	  Context MyContext;
	  
	  // Constructor
     public ImageAdapter(Context _MyContext){
              MyContext = _MyContext;
           }
           
           @Override
     public int getCount(){
        	   return 16;
     }

           @Override
     public View getView(int position, View convertView, ViewGroup parent){
        	   ImageView imageView;
               if(convertView == null) {
                   imageView = new ImageView(MyContext);
                   imageView.setLayoutParams(new GridView.LayoutParams(147,147));
                   imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
//                   imageView.setPadding(1, 1, 1, 1);
               }
               else {
                   imageView = (ImageView) convertView;
               }
               imageView.setImageResource(imageIDs[position]);
               return imageView;
           }

           @Override
           public Object getItem(int arg0) {
              // TODO Auto-generated method stub
              return null;
           }

           @Override
           public long getItemId(int arg0) {
              // TODO Auto-generated method stub
              return 0;
           }
        }
//--------------------------------------------------------------------
      	
    // After touching the SplashScreen
	public void onAfterSplashScreen()
	{
    	setContentView(R.layout.activity_main);
        Button button1 = (Button) findViewById(R.id.button1);
        Button button2 = (Button) findViewById(R.id.button2);
        Button button3 = (Button) findViewById(R.id.button3);
        Button button4 = (Button) findViewById(R.id.button4);
        Button button5 = (Button) findViewById(R.id.button5);
        Button button6 = (Button) findViewById(R.id.button6);
        Button button7 = (Button) findViewById(R.id.button7);
        Button button8 = (Button) findViewById(R.id.button8);
        
        Button buttonPlay = (Button) findViewById(R.id.Buttonplay);
        buttonStop = (Button) findViewById(R.id.Buttonstop);
        
    	_b1click = new OnClickListener(){
			public void onClick(View v){
				_musicThreadsArray[0].run();
			}
		};
		_b2click = new OnClickListener(){
          	public void onClick(View v){
           		_musicThreadsArray[1].run();
       		}
       	};
        _b3click = new OnClickListener(){
           	public void onClick(View v){
           		_musicThreadsArray[2].run();
       		}
       	};
        _b4click = new OnClickListener(){
           	public void onClick(View v){
           		_musicThreadsArray[3].run();
       		}
       	};
        _b5click = new OnClickListener(){
           	public void onClick(View v){
           		_musicThreadsArray[4].run();
       		}
       	};
        _b6click = new OnClickListener(){
           	public void onClick(View v){
           		_musicThreadsArray[5].run();
       		}
       	};
        _b7click = new OnClickListener(){
           	public void onClick(View v){
           		_musicThreadsArray[6].run();
       		}
       	};
        _b8click = new OnClickListener(){
           	public void onClick(View v){
           		_musicThreadsArray[7].run();
       		}
       	};
        button1.setOnClickListener(_b1click);
        button2.setOnClickListener(_b2click);
        button3.setOnClickListener(_b3click);
        button4.setOnClickListener(_b4click);
        button5.setOnClickListener(_b5click);
        button6.setOnClickListener(_b6click);
        button7.setOnClickListener(_b7click);
        button8.setOnClickListener(_b8click);
        buttonPlay.setOnClickListener(playListener);

        GridView gridview = (GridView) findViewById(R.id.gridview);
        gridview.setAdapter(new ImageAdapter(this));
             
        gridview.setOnItemClickListener(new OnItemClickListener()
        {
            public void onItemClick(AdapterView<?> parent, View v, int position, long id)
            {
                Toast.makeText(getBaseContext(), "Hello you "+ (position+1)+
                               " KIKOU", Toast.LENGTH_SHORT).show();
            }
        });
	}	
	
//--------------------------------------------------------------------
	
    public void onEdition()
    {
    	onAfterSplashScreen();
    }

//--------------------------------------------------------------------
  	
	public void onPlay()
	{
		setContentView(R.layout.activity_play);
        System.out.println("JE PASSE dans LE PLAY");
		buttonStop = (Button) findViewById(R.id.Buttonstop);
		buttonStop.setOnClickListener(stopListener);
	}

//--------------------------------------------------------------------
	
	public void onLoad()
	{
		
	}
//--------------------------------------------------------------------	
	@Override
    public void onCreate(Bundle savedInstanceState) 
    {
    	super.onCreate(savedInstanceState);
    	setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    	setContentView(R.layout.startscreen);
    	    	
		_musicThreadsArray = new MyMusicThread[8];
		_mainLayout = (RelativeLayout) findViewById(R.id.splashScreen);
		
		_musicThreadsArray[0] = new MyMusicThread(MediaPlayer.create(this, R.raw.up));
		_musicThreadsArray[0].start();
	//	dR0 = new DrawRectangle(this, Color.BLACK, 500, 500, 550, 1000);
//		_mainLayout.addView(dR0);

		_musicThreadsArray[1] = new MyMusicThread(MediaPlayer.create(this, R.raw.dwqdwqcdcc));
		_musicThreadsArray[1].start();
	//	dR1 = new DrawRectangle(this, Color.BLUE, 550, 600, 600, 1000);
//		_mainLayout.addView(dR1);

		_musicThreadsArray[2] = new MyMusicThread(MediaPlayer.create(this, R.raw.dwqwdqdwq));
		_musicThreadsArray[2].start();
	//	dR2 = new DrawRectangle(this, Color.GREEN, 600, 450, 650, 1000);
//		_mainLayout.addView(dR2);
		
		_musicThreadsArray[3] = new MyMusicThread(MediaPlayer.create(this, R.raw.edjdfj));
		_musicThreadsArray[3].start();
	//	dR3 = new DrawRectangle(this, Color.CYAN, 650, 500, 700, 1000);
//		_mainLayout.addView(dR3);
		
		_musicThreadsArray[4] = new MyMusicThread(MediaPlayer.create(this, R.raw.eeee));
		_musicThreadsArray[4].start();
	//	dR4 = new DrawRectangle(this, Color.BLACK, 700, 550, 750, 1000);
//		_mainLayout.addView(dR4);
		
		_musicThreadsArray[5] = new MyMusicThread(MediaPlayer.create(this, R.raw.ewew));
		_musicThreadsArray[5].start();
	//	dR5 = new DrawRectangle(this, Color.BLUE, 750, 450, 800, 1000);
//		_mainLayout.addView(dR5);
		
		_musicThreadsArray[6] = new MyMusicThread(MediaPlayer.create(this, R.raw.tat));
		_musicThreadsArray[6].start();
	//	dR6 = new DrawRectangle(this, Color.GREEN, 800, 600, 850, 1000);
//		_mainLayout.addView(dR6);
		
		_musicThreadsArray[7] = new MyMusicThread(MediaPlayer.create(this, R.raw.titi));
		_musicThreadsArray[7].start();
	//	dR7 = new DrawRectangle(this, Color.MAGENTA, 850, 550, 900, 1000);
//		_mainLayout.addView(dR7);

		_mainLayout.setOnClickListener(splashListener);
    }
}
